package com.hns.can.master.service;/**
 * Created by zhangyu on 2017/4/14.
 */

import com.hns.can.master.entity.BaseCarModelInfoEntity;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 车型信息service
 * @author zhangy
 * @version Revision 2.0.0
 * @email: zhangy2222z@sina.cn
 * @create 2017-04-14 9:13
 **/
@Service
public class BaseCarModelInfoService extends CommonService{

    /**
     * 获取车型分页
     * @param param
     * @return
     * @throws Exception
     */
    public Pagination getCarModelInfoPage(BaseCarModelInfoEntity param) {
        Pagination page = this.myBatisPage("baseCarModel.getBaseCarModelDataCnt", "baseCarModel.getBaseCarModelData", param, BaseCarModelInfoEntity.class);
        return page;
    }

    /**
     * 查询导出车型数据
     * @param param
     * @return
     */
    public List<BaseCarModelInfoEntity> getCarModelInfoList(BaseCarModelInfoEntity param) {
        List<BaseCarModelInfoEntity> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("baseCarModel.getBaseCarModelData", param), BaseCarModelInfoEntity.class);
        return list;
    }

    /**
     * 添加车型
     * @param param
     * @return
     */
    @Transactional
    public void addCarModelInfo(BaseCarModelInfoEntity param, String userId) throws Exception{
        HashMap map = new HashMap();
        map.put("carModelName", param.getCarModelName());
        map.put("enableSt", "0");
        List list = this.commonEntityDao.findByParamsIgnoreNullValue(BaseCarModelInfoEntity.class, map);
        if(PubUtil.isNotEmpty(list)){
            throw new BusinessException(MsgType.ERROR.getFlag(), "车型名称已存在！");
        }

        param.setId(UtilString.getUUID());
        param.setEnableSt("0");
        param.setCrtTime(new Date());
        param.setCrtUser(userId);
        param.setUpdateTime(new Date());
        param.setUpdateUser(userId);
        this.commonEntityDao.save(param);
    }

    /**
     * 删除车型
     * @param ids
     * @return
     */
    @Transactional
    public void delCarModelInfo(String[] ids) {
        if(PubUtil.isNotEmpty(ids)){
//            this.commonEntityDao.delete(BaseCarModelInfoEntity.class, ids);
            for(String id : ids){
                BaseCarModelInfoEntity carModel = this.commonEntityDao.find(id, BaseCarModelInfoEntity.class);
                carModel.setEnableSt("1");
                this.commonEntityDao.update(carModel);
            }
        }
    }


    public List getCarModelSelection(String corpId) {
        HashMap map = new HashMap();
        map.put("corpId", corpId);
        List list = this.commonEntityDao.findByParamsIgnoreNullValue(BaseCarModelInfoEntity.class, map);
//        this.sqlMapDao.selectList("baseCarModel.", map);
        return list;
    }
}
